<template>
  <view class="launch-page">
    <!-- 可以添加启动页的 UI，比如 logo 等 -->
  </view>
</template>

<script>
import { checkToken } from '../../src/utils/auth'

export default {
  onLoad() {
    this.checkAndRedirect()
  },
  methods: {
    async checkAndRedirect() {
      try {
        const token = uni.getStorageSync('token')
        if (token) {
          // 验证 token 是否有效
          const res = await this.verifyToken()
          if (res.valid) {
            uni.reLaunch({
              url: '/pages/wode-dongtai/wode-dongtai'
            })
          } else {
            uni.removeStorageSync('token')
            uni.reLaunch({
              url: '/pages/wode-kong/wode-kong'
            })
          }
        } else {
          uni.reLaunch({
            url: '/pages/wode-kong/wode-kong'
          })
        }
      } catch (error) {
        console.error('启动页检查失败：', error)
        uni.reLaunch({
          url: '/pages/wode-kong/wode-kong'
        })
      }
    },
    
    // 验证 token 是否有效
    async verifyToken() {
      try {
        const res = await uni.request({
          url: `${process.env.NODE_ENV === 'development' 
            ? 'http://127.0.0.1:5000/api' 
            : 'http://127.0.0.1:5000/api'}/user/info/detail`,
          method: 'POST',
          header: {
            'Authorization': uni.getStorageSync('token')
          }
        })
        
        return {
          valid: res.data.code === 200
        }
      } catch (error) {
        console.error('验证token失败：', error)
        return { valid: false }
      }
    }
  }
}
</script>

<style scoped>
.launch-page {
  min-height: 100vh;
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style> 